package NetModel.okhttp.requset;

import android.support.annotation.NonNull;


import java.util.concurrent.TimeUnit;

import NetModel.okhttp.OkHttpClientManger;
import okhttp3.Call;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;

/**
 * Created by Administrator on 2016/10/27.
 */
public class PostStringRequestBuilder extends BaseRequest<PostStringRequestBuilder> {

    private String context;

    @Override
    public Call build() {
        Request req = new Request.Builder()
                .post(addStringBody(context))
                .url(url)
                .headers(addHead(heads))
                .tag(tag)
                .build();
        Call call = OkHttpClientManger.getInstance().getClient().newBuilder().
                connectTimeout(connectTimeOut > 0 ? connectTimeOut : OkHttpClientManger.DEFAULT_CONNECT_TIMEOUT, TimeUnit.MILLISECONDS).
                readTimeout(readTimeOut > 0 ? readTimeOut : OkHttpClientManger.DEFAULT_READ_TIMEOUT, TimeUnit.MILLISECONDS).
                writeTimeout(writeTimeOut > 0 ? writeTimeOut : OkHttpClientManger.DEFAULT_WRITE_TIMEOUT, TimeUnit.MILLISECONDS).
                build().newCall(req);
        return call;
    }

    private RequestBody addStringBody(@NonNull String context) {
        return RequestBody.create(MediaType.parse("application/json; charset=utf-8"), context);
    }


    public <T extends BaseRequest> T context(String context) {
        this.context = context;
        return (T) this;
    }


}
